package com.lyfx.ly.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.lyfx.common.constant.RedisConstant;
import com.lyfx.common.pojo.CatPojo;
import com.lyfx.common.pojo.CoursePojo;
import com.lyfx.common.valid.AddGroup;
import com.lyfx.common.valid.EditGroup;
import com.lyfx.common.valid.EditStatusGroup;
import com.lyfx.ly.entity.ClassCategoryEntity;
import com.lyfx.ly.entity.ClassCourseEstypeEntity;
import com.lyfx.ly.service.ClassCourseEstypeService;
import com.lyfx.ly.vo.CourseRouteVo;
import com.lyfx.ly.vo.CourseVo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lyfx.ly.entity.ClassCourseEntity;
import com.lyfx.ly.service.ClassCourseService;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.R;

import javax.validation.Valid;


/**
 * **课程表**
 *
 * @author lsy
 * @email lsy@gmail.com
 * @date 2020-07-05 18:03:59
 */
@RestController
@RequestMapping("ly/classcourse")
public class ClassCourseController {
    @Autowired
    private ClassCourseService classCourseService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    ClassCourseEstypeService classCourseEstypeService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ly:classcourse:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = classCourseService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/all")
    @RequiresPermissions("ly:classcourse:list")
    public R listAll(){
        List<ClassCourseEntity> list = classCourseService.list(null);
        List<CourseRouteVo> collect = list.stream().map(item -> {
            CourseRouteVo routeVo = new CourseRouteVo();
            BeanUtils.copyProperties(item, routeVo);
            return routeVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

    /**
     * 信息
     */
    @RequestMapping("list/info")
    @RequiresPermissions("ly:classcourse:info")
    public R listInfo(@RequestBody Long[] courseIds){
        List<ClassCourseEntity> entities = classCourseService.listByIds(Arrays.asList(courseIds));
        List<CourseVo> collect = entities.stream().map(item -> {
            CourseVo courseVo = new CourseVo();
            courseVo.setCourseId(item.getCourseId());
            courseVo.setCourseName(item.getName());
            return courseVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{courseId}")
    @RequiresPermissions("ly:classcourse:info")
    public R info(@PathVariable("courseId") Long courseId){
		ClassCourseEntity classCourse = classCourseService.getById(courseId);

        return R.ok().put("classCourse", classCourse);
    }

    /**
     * 保存
     * @Valid校验注解
     * 在校验的bean后紧跟一个BindingResult,封装了返回的错误结结果
     * , BindingResult result
     * 保存的时候，在estype表里存一份
     */
    @RequestMapping("/save")
    @RequiresPermissions("ly:classcourse:save")
    @Transactional
    public R save(@Validated({AddGroup.class}) @RequestBody ClassCourseEntity classCourse){
        classCourseService.save(classCourse);
        //存入estype
        ClassCourseEstypeEntity entity = new ClassCourseEstypeEntity();
        entity.setCourseId(classCourse.getCourseId());
        classCourseEstypeService.save(entity);
        //存入redis
        saveImgMysqlRedis(classCourse.getLogo());
        return R.ok();
    }

    @GetMapping("/my/test")
    public R testMy(@RequestParam("string") String string){
        System.out.println(string);
        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping("/beach/save/class")
    public R beachSaveCourse(@RequestBody List<CoursePojo> coursePojos){
        List<ClassCourseEntity> collect = coursePojos.stream().map(item -> {
            ClassCourseEntity classCourse = new ClassCourseEntity();
            BeanUtils.copyProperties(item, classCourse);
            //存入redis
            saveImgMysqlRedis(classCourse.getLogo());
            return classCourse;
        }).collect(Collectors.toList());
        classCourseService.saveBatch(collect);
        //存入estype
        for (ClassCourseEntity entitys : collect) {
            ClassCourseEstypeEntity entity = new ClassCourseEstypeEntity();
            entity.setCourseId(entitys.getCourseId());
            classCourseEstypeService.save(entity);
        }

        return R.ok();
    }

    /**
     * 数据库中的图片地址存入redis
     */
    private void saveImgMysqlRedis(String logo) {
        if(!StringUtils.isEmpty(logo)){
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_OSS_DB_RESOURCES, logo);
        }
    }
    //TODO 修改的时候数据库中图片发生变化,redis还是原来的
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ly:classcourse:update")
    public R update(@Validated({EditGroup.class}) @RequestBody ClassCourseEntity classCourse){
		classCourseService.updateDetail(classCourse);
        return R.ok();
    }
    /**
     * 修改状态信息
     */
    @RequestMapping("/update/status")
    @RequiresPermissions("ly:classcourse:update")
    public R updateStatus(@Validated({EditStatusGroup.class}) @RequestBody ClassCourseEntity classCourse){
        classCourseService.updateById(classCourse);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ly:classcourse:delete")
    public R delete(@RequestBody Long[] courseIds){
		classCourseService.removeByIds(Arrays.asList(courseIds));

        return R.ok();
    }

}
